_realname=libipt
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgbase="mingw-w64-${_realname}"
pkgver=2.0.6.r0.gba98732
pkgrel=1
pkgdesc='Intel(R) Processor Trace decoder library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/intel/libipt'
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
           #  'pandoc' # Only required for building the manpage, we likely don't want that
             "${MINGW_PACKAGE_PREFIX}-cc"
             'git')
_commit='ba98732fc7c3269e06b8587bce27ac87c847c0ff'
source=("${_realname}"::"git+${url}#commit=${_commit}")
sha256sums=('SKIP')

pkgver() {
  cd "${srcdir}/${_realname}"

  git describe --long --tags --abbrev=7 | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DDEVBUILD=OFF \
      -DFEATURE_THREADS=ON \
      -DFEATURE_ELF=ON \
      -DMAN=OFF \
      -DPTDUMP=ON \
      -DPTTC=ON \
      -DPTUNIT=ON \
      -DPTXED=OFF \
      -DSIDEBAND=ON \
      ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}/${_realname}/README"  "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README"
  install -Dm644 "${srcdir}/${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
